/**
 * 
 * @authors youzhao.zhou@pandauu.com
 * @date    2016-09-22 13:31:58
 * @version $Id$
 * 
 * 客服页面消息请求
 * 
  页面加载完成后，中间件通知数据层启动Ajax请求，中间件递归等待请求返回，返回后，启动数据中间件，通知数据整理层对数据整理，中间件递归等待整理结束，结束后，通知视图层，根据数据渲染页面，渲染结束后，通知中间件渲染结束，清理变量


 * 
 */

;(function(root,factory){

  if(typeof exports === 'object' && typeof module === 'object'){
    
    factory(exports);

  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['exports'], factory);
  } else if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
    // CommonJS
    factory(exports);
  } else {
    // Browser globals
    if (!root._s) {
      root._s = {};
    }
    factory(root);
  }
})(typeof window !== 'undefined' ? window : this ,function(exports, undefined){
  'use strict';

  function moduleInit($, log, conf, utils, options){

    var urlConf = conf._conf;       //存储配置信息
    var pubsub = utils.pubSub()||{};
    var callback = {};
    var callbackFuc = utils.callbackFuc;
    var setUrl = utils.setUrl;

    function init(identifier, callbacks){
      
      callback[identifier] = [callbacks];

      pubsub.addEvent(identifier, function(data){
        console.log(callback[identifier]);
        callbackFuc(callback[identifier],data);
      });

      return {
        set: function(identifier, options){
          receiver(identifier, options);
        }
      };
      
    }

    function receiver(identifier, options){
      var deferr = null;
      switch(identifier){
        case 'history':
          deferr = getHistory(options);
        break;
        case 'quick':
          deferr = getQuickData(options);
        break;
      }
      
      deferr.then(function(data){
        /*
          回调通知数据获取完毕

        */
        pubsub.fireEvent(identifier,data);
      });
    };

    function getHistory(options){
      /*
        获取历史消息
      */
      var deferr = $.ajax({
        url: urlConf.apiRoot + urlConf.apiPre.one + urlConf.api.msgList,
        type: 'GET',
        dataType: 'json',
        data: {
          thread_id : options.thread_id
        }
      })
      .done(function(data) {
        if (data.code === 0) {

          for(var k in data.data){
            if (data.data[k].type === '1') {

              data.data[k].msg = setUrl(data.data[k].msg);
            }else{
              data.data[k].msg = urlConf.root + data.data[k].msg;
              data.data[k].thumb = data.data[k].msg.replace('uploads/kf/','uploads/kf/');
            }
          }

        }else if(data.code === 100){
          window.location.href = urlConf.root + urlConf.static + 'login.php';
        }
          
      })
      .fail(function() {
        log._log("error");
      })
      .always(function() {
        log._log("complete");
      });
      
      return deferr;
    }
    /*
      获取快捷回复列表
    */
    function getQuickData(options){

      var deferr = $.ajax({
        url: urlConf.apiRoot + urlConf.apiPre.one + urlConf.api.quickReplyList,
        type: 'GET',
        dataType: 'json'
      })
      .done(function(data) {
        if (data.code === 0) {

        }
          
      })
      .fail(function() {
        log._log("error");
      })
      .always(function() {
        log._log("complete");
      });
      
      return deferr;
    }

    return {
      receiver: init //接收通知
    };
  }

  exports.msgItem = moduleInit;

});